<template>
  <div style="margin-top: 100px;height: 680px">
    <el-row>
      <el-col :xs="{span: 22,offset:1}" :sm="{span: 18,offset:3}" :md="{span: 18,offset:3}" :lg="{span: 18,offset:3}" :xl="{span: 8,offset:8}" align="center">
        <h1>登录</h1>
      </el-col>
    </el-row>
    <el-row style="height: 100%">
      <el-col :xs="{span: 22,offset:1}" :sm="{span: 18,offset:3}" :md="{span: 18,offset:3}" :lg="{span: 18,offset:3}" :xl="{span: 8,offset:8}">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm">
          <el-form-item prop="username">
            <el-input type="text" placeholder="请输入账号" v-model="ruleForm.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" placeholder="请输入密码" v-model="ruleForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item align="center">
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button type="primary" @click="() => {this.$router.push({path:'/register'})}">注册</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { login } from '../../api/auth'
import { fucCheckTEL, isEmail, isQQ } from '../../utils/validate'
import { md5 } from 'md5js'

export default {
  name: 'Login',
  data () {
    var checkUsername = (rule, value, callback) => {
      if (isEmail(value) || isQQ(value) || fucCheckTEL(value)) {
        callback()
      } else {
        callback(new Error('用户名错误'))
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      }
      if (value.length < 6) {
        callback(new Error('密码长度必须大于6位'))
      }
      if (this.ruleForm.checkPass !== '') {
        this.$refs.ruleForm.validateField('checkPass')
      }
      callback()
    }
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        username: [
          { validator: checkUsername, trigger: 'blur' }
        ]
      },
      replaceUrl: '/list'
    }
  },
  mounted () {
    if (localStorage.getItem(process.env.VUE_APP_TOKEN)) {
      this.$router.replace({
        path: this.replaceUrl
      })
    }
    this.isWechat()
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login({
            username: this.ruleForm.username,
            password: md5(this.ruleForm.password, 32)
          }).then((res) => {
            if (res.code === 1) {
              localStorage.setItem(process.env.VUE_APP_TOKEN, res.ext.token)
              this.$store.state.token = res.data.token
              this.$router.push({
                path: this.replaceUrl
              })
            } else {
              this.$message({
                message: res.msg,
                type: 'error'
              })
            }
          }).catch(err => {
            this.$message({
              message: err,
              type: 'error'
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
